package org.test.client.ui.component.window;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;

import org.jb2011.ninepatch4j.NinePatch;
import org.test.client.ui.skinui.NinePatchHelper;
import org.test.client.ui.util.UIUtil;
import org.test.debug.Debug;

/**
 * 因为Beauty Eye 已经 实现了一种toolTip factory‘  
 * 故在此 直接继承jwindow 需要增加 这种toolTip的 
 * 需要给组件增加org.test.client.listener.ToolTipMouseListener即可
 * 不过 此类 toolTip 是重量级popup 占用资源稍大
 * 用法 @see org.test.client.ui.content.uifeature.ToolTipPanel
 *  
 * @author Guodeqi
 *
 */
public class BlackToolTip extends JWindow{
	//暂时没用
//	public float alpha = (float) 1.0;
	
	public String tooltipStr = "";
	public Font tooltipFont = new Font(Font.DIALOG, Font.PLAIN, 12);
	public Color fontColor = Color.black;
	public boolean isHide = true;
	public int nowHeight = 0;
	public int nowWidth = 0;
	
	public BlackToolTipPanel tpPanel = null;
	public JComponent toolTipComp = null;
	
	public int windoww = -1;
	public int windowh = -1;
	public boolean isInitSize = false;
	
	public BlackToolTip(String toolTipString,JComponent comp){
		this.tooltipStr = toolTipString;
		this.toolTipComp = comp;
		tpPanel = new BlackToolTipPanel(this);
		this.add(tpPanel);
		this.setAlwaysOnTop(true);
		UIUtil.setWindowOpaque(this, false);
	}
	
	public void showToolTip(){
		this.pack();
		if(toolTipComp == null){
			this.setLocation(500,400);
		}else{
			Point point = new Point(0,0);
			SwingUtilities.convertPointToScreen(point, toolTipComp);
			int x = point.x;
			int y = point.y;
			if(!isInitSize){
				UIUtil.setWindowOpacity(this, 0.00f);
				this.setVisible(true);
				windoww = this.getWidth();
				windowh = this.getHeight();
//				Debug.debug(windoww + "||||||"+windowh);
			}
			
			y = y-windowh+tpPanel.marginTop;//+tpPanel.picBotBorder
			x = x-(windoww-toolTipComp.getWidth())/2;
			this.setLocation(x , y );
		}
		UIUtil.fadeInWindow(this,10);
		isHide = false;
	}
	
	public void showToolTip(int x, int y ){
		this.pack();
		this.setLocation(x,y);
		UIUtil.fadeInWindow(this,10);
		isHide = false;
	}
	
	public void hideToolTip(){
		UIUtil.fadeOutWindow(this,10);
		isHide = true;
	}
	
//	public static void main(String[] args) {
//		BlackToolTip t = new BlackToolTip("这是一个测试这是一个测试这是一个测试",null);
//		t.showToolTip();
//	}


	public void setTooltipStr(String tooltipStr) {
		if(this.tooltipStr.length()!= tooltipStr.length()){
			isInitSize = false;
		}
		this.tooltipStr = tooltipStr;
	}

}

class BlackToolTipPanel extends JPanel{
	BlackToolTip w ;
	String imgPath = "imgs/tipMax.9.png";
	BufferedImage bi = null;

	int marginTop = 5;
	int marginLeft = 10;
	//根据NinePatch决定
	int picTopBorder = 3;
	int picBotBorder = 14;
	
	public BlackToolTipPanel(BlackToolTip w){
		this.w = w;
		this.setOpaque(false);
	}
	
	protected void paintComponent(Graphics g) {
		try {
			File file = new File(imgPath);
			NinePatch npImg = NinePatchHelper.createNinePatch(file.toURI().toURL(), false);
			//创建graphics2d副本
			Graphics2D g2d = (Graphics2D) g.create();
			UIUtil.setAntiAliasing(g2d, true);
			g2d.setFont(w.tooltipFont);
			g2d.setColor(w.fontColor);
			int txtWidth = UIUtil.getTxtWidth(g2d, w.tooltipStr);
			int txtHeight = UIUtil.getTxtHeight(g2d);
			
			int width = txtWidth+marginLeft*2;
			int height = txtHeight+marginTop*2+picTopBorder+ picBotBorder;
			
			//创建通用透明图
//			BufferedImage img =Icons.deCfg.createCompatibleImage(width,height,Transparency.TRANSLUCENT);
//			Graphics2D g2 = img.createGraphics();
//			g2.setComposite(AlphaComposite.SrcOver);
//			//暂时没有搞懂是干啥用的
//			g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
			npImg.draw(g2d, 0, 0, width, height);
//			g2.dispose();

//			g2d.drawImage(img, 0, 0, width,height,this);
			
			int x = 10;
			int y = height/2 -  UIUtil.getTxty(g2d) + 3;
			g2d.drawString(w.tooltipStr, x, y);
			
			g2d.dispose();
			
//			this.setPreferredSize(new Dimension(width,height));
			w.setSize(width, height);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}

